﻿using System;
using System.Collections;
using System.Web.Mvc;
using MvcApplication1.Utility;
using RIMS.Lib.Models;
using MvcApplication1.Actions.EmployeeActions;
using IList = MvcApplication1.Actions.EmployeeActions.IList;

namespace MvcApplication1.Controllers
{
    public class EmployeeController : Controller
    {
        private readonly IIndex _index;
        private readonly ICreate _create;
        private readonly ISelect _select;
        private readonly IUpdate _update;
        private readonly IDelete _delete;
        private readonly IList _list;
        private readonly ISearch _search;

        public EmployeeController(IIndex index, 
            ICreate create, 
            ISelect select, 
            IUpdate update, 
            IDelete delete, 
            IList list, 
            ISearch search)
        {
            _index = index;
            _create = create; 
            _select = select;
            _update = update;
            _delete = delete;
            _list = list;
            _search = search;
        }
        
        public ActionResult Index(FormCollection collection)
        {
            return View(_index.Execute(collection));
        }

        [HttpPost]
        public ActionResult Create(Employee employee, FormCollection collection)
        {
            return Json(_create.Execute(employee, collection));
        }

        [HttpPost]
        public ActionResult Select(Guid oid)
        {
            return Json(_select.Execute(oid));
        }

        [HttpPost]
        public ActionResult Update(Employee employee, Guid oid)
        {
            return Json(_update.Execute(employee, oid));
        }

        [HttpPost]
        public ActionResult Delete(Guid oid)
        {
            return Json(_delete.Execute(oid));
        }

        [HttpPost]
        public ActionResult List(FormCollection collection)
        {
            Hashtable executeResult = !string.IsNullOrEmpty(collection[Tools.QTYPE]) ?
                _search.Execute(collection) : _list.Execute(collection);
            return Json(executeResult);
        }
    }
}